package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"path/filepath"
)

//https://update.todesk.com/setup_20201026a.exe

func main() {
	//url := "https://github.com/ovh/cds/archive/0.48.0.zip"
	url := "https://gitee.com/wailscn/wails-plugin-update/releases/download/0.0.1/update_windows_amd64.exe"
	DownloadFile(url, "/download/")

}

type Reader struct {
	io.Reader
	Total   int64
	Current int64
}

func DownloadFile(url string, local string) {
	res, err := http.Get(url)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()
	_, fileName := filepath.Split(url)
	filelocal := "." + local + fileName
	f, err := os.Create(filelocal)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	reader := &Reader{
		Reader: res.Body,
		Total:  res.ContentLength,
	}
	io.Copy(f, reader)
}
func (r *Reader) Read(p []byte) (n int, err error) {
	n, err = r.Reader.Read(p)
	r.Current += int64(n)
	fmt.Printf("\r进度 %.2f%%", float64(r.Current*10000/r.Total)/100)
	return
}
